package me.hetong.caigou.ui.chessboard;

import cn.hutool.core.collection.CollectionUtil;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import me.hetong.caigou.entity.Chess;
import me.hetong.caigou.util.FenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChessboardWarImage {

    // 偏移量
    public static final int PY = (ChessboardWarOnlyBoard.GRID_SIZE - ChessboardWarOnlyBoard.CHESS_SIZE) / 2 + ChessboardWarOnlyBoard.GRID_SIZE / 4;
    public static List<Chess> chessList;
    public static Map<String, Image> imageMap = new HashMap<>(64);
    public static List<ImageView> fromTempList = new ArrayList<>();
    public static List<ImageView> toTempList = new ArrayList<>();

    static {
        {
            /*
            先把需要用的图片缓存起来，免得每次都加载
             */
            chessList = FenUtils.fenToChessList(null);
            for (Chess chess : chessList) {
                String key = (chess.getType() + chess.getK()).toLowerCase().replace("w", "r");
                String url = "image/chessboard/style/default/" + key + ".png";
                imageMap.put(key, new Image(url));
            }

            imageMap.put("from", new Image("image/chessboard/from.png"));
            imageMap.put("to", new Image("image/chessboard/to.png"));
        }
    }

    /**
     * 设置初始位置图标
     *
     * @param x
     * @param y
     */
    public static void setFromImageViewLocation(int x, int y) {
        clearFromLocation();
        ImageView imageView = createImageView("from", x, y);
        fromTempList.add(imageView);
    }

    /**
     * 设置目标位置图标
     *
     * @param x
     * @param y
     */
    public static void setToImageViewLocation(int x, int y) {
        clearToLocation();
        ImageView imageView = createImageView("to", x, y);
        toTempList.add(imageView);
    }

    /**
     * 清空位置坐标
     */
    public static void clearImageViewLocation() {
        clearFromLocation();
        clearToLocation();
    }

    public static void clearFromLocation() {
        for (ImageView imageView : fromTempList) {
            ChessBoardWar.ROOT.getChildren().remove(imageView);
        }
    }

    public static void clearToLocation() {
        for (ImageView imageView : toTempList) {
            ChessBoardWar.ROOT.getChildren().remove(imageView);
        }
    }

    public static void initChessList(String fen) {

        // 隐藏现在的所有棋子
        if (CollectionUtil.isNotEmpty(chessList)) {
            for (Chess chess : chessList) {
                ChessBoardWar.ROOT.getChildren().remove(chess.getImageView());
            }
        }

        chessList = FenUtils.fenToChessList(fen);

        for (Chess chess : chessList) {
            String key = (chess.getType() + chess.getK()).toLowerCase().replace("w", "r");
            ImageView imageView = createImageView(key, chess.getX(), chess.getY());
            chess.setImageView(imageView);
        }
    }

    public static ImageView createImageView(String key, int x, int y) {

        Image image = imageMap.get(key);

        ImageView imageView = new ImageView(image);
        imageView.setX(x * ChessboardWarOnlyBoard.GRID_SIZE + PY);
        imageView.setY(y * ChessboardWarOnlyBoard.GRID_SIZE + PY);
        imageView.setFitWidth(ChessboardWarOnlyBoard.CHESS_SIZE);
        imageView.setFitHeight(ChessboardWarOnlyBoard.CHESS_SIZE);

        // 图片阴影
        DropShadow shadow = new DropShadow();

        if (key.equals("from")) {
//            imageView.setOpacity(0.84);
            shadow.setOffsetX(1);
            shadow.setOffsetY(1);
            imageView.setEffect(shadow);
        } else if (key.equals("to")) {
//            imageView.setOpacity(0.84);
        } else {
            shadow.setOffsetX(4);
            shadow.setOffsetY(4);
            imageView.setEffect(shadow);
        }

        ChessBoardWar.ROOT.getChildren().add(imageView);

        return imageView;
    }

    public static Chess getChess(int x, int y) {
//        System.out.println(JSONUtil.toJsonStr(chessList));
        for (Chess chess : chessList) {
            if (chess.getX() == x && chess.getY() == y) {
                return chess;
            }
        }
        return null;
    }
}